//
//  CircularProgressView.swift
//  JTYP
//
//  Created by Groot on 2017/11/29.
//  Copyright © 2017年 inspur. All rights reserved.
//

class CircularProgressView: UIView {
    
    // 进度
    var percent: CGFloat = 0 {
        didSet {
            if oldValue > 1 {
                percent = 1
            }else if oldValue < 0 {
                percent = 0
            }
            reset()
        }
    }
    // 内外圆环距离
    let circleMargin: CGFloat = 8
    // 视图内边距
    let padding: CGFloat = 1
    // dashPattern
    let innerDashPatternFirst = 2
    let innerDashPatternSecond = 3
    let outerDashPatternFirst = 2
    let outerDashPatternSecond = 4
    // 线宽
    let innerCircleLineWidth: CGFloat = 10
    let outerCircleLineWidth: CGFloat = 2
    let strokeColor = UIColor.init(red: 36/255.0, green: 199/255.0, blue: 137/255.0, alpha: 1.0).cgColor
    let defaultColor = UIColor.init(white: 0.8, alpha: 0.6)
    // 圆角
    private lazy var innerCircleRadius: CGFloat = {[unowned self] in
        return (CGFloat.minimum(self.bounds.width , self.bounds.height) - padding * 2 - innerCircleLineWidth * 2 - circleMargin * 2) / 2
    }()
    private lazy var outerCircleRadius: CGFloat = {
        return (CGFloat.minimum(self.bounds.width , self.bounds.height) - outerCircleLineWidth * 2 - circleMargin * 2) / 2
    }()
    // 起始角度
    private var startAngle: CGFloat = -270
    // 结束角度
    private var endAngle: CGFloat = 89
    // 内圈进度条
    private lazy var bottomShapeLayer: CAShapeLayer = {[unowned self] in
        var layer = CAShapeLayer()
        layer.frame = self.bounds
        layer.path = self.getBezierPath(radius: innerCircleRadius).cgPath
        layer.lineCap = kCALineCapButt
        layer.lineDashPattern = [NSNumber.init(value: innerDashPatternFirst), NSNumber.init(value: innerDashPatternSecond)]
        layer.backgroundColor = UIColor.clear.cgColor
        layer.lineWidth = innerCircleLineWidth
        layer.strokeColor = defaultColor.cgColor
        layer.fillColor = UIColor.clear.cgColor
        return layer
    }()
    // 内圈遮照视图
    private lazy var upperMaskShapeLayer: CAShapeLayer = {
        var layer = CAShapeLayer()
        layer.frame = self.bounds
        layer.path = self.getBezierPath(radius: innerCircleRadius).cgPath
        layer.lineCap = kCALineCapButt
        layer.strokeStart = 0
        layer.strokeEnd = 0
        layer.lineDashPattern = [NSNumber.init(value: innerDashPatternFirst), NSNumber.init(value: innerDashPatternSecond)]
        layer.backgroundColor = UIColor.clear.cgColor
        layer.lineWidth = innerCircleLineWidth
        layer.strokeColor = UIColor.red.cgColor
        layer.fillColor = UIColor.clear.cgColor
        return layer
    }()
    // 内圈渐变涂层
    private lazy var upperGradientLayer: CAGradientLayer = {
        var layer = CAGradientLayer()
        layer.frame = self.bounds
        layer.shadowPath = self.getBezierPath(radius: innerCircleRadius).cgPath
        layer.backgroundColor = UIColor.clear.cgColor
        layer.startPoint = CGPoint.init(x: 0, y: 1)
        layer.endPoint = CGPoint.init(x: 1, y: 0)
        layer.colors = [strokeColor, strokeColor]
        return layer
    }()
    // 外圈进度条
    private lazy var progressBottomLayer: CAShapeLayer = {[unowned self] in
        var layer = CAShapeLayer()
        layer.frame = self.bounds
        layer.path = self.getBezierPath(radius: outerCircleRadius).cgPath
        layer.lineCap = kCALineCapRound
        layer.lineDashPattern = [NSNumber.init(value: outerDashPatternFirst), NSNumber.init(value: outerDashPatternSecond)]
        layer.backgroundColor = UIColor.clear.cgColor
        layer.lineWidth = outerCircleLineWidth
        layer.strokeColor = UIColor.clear.cgColor
        layer.fillColor = UIColor.clear.cgColor
        return layer
        }()
    // 外圈遮照视图
    private lazy var progressUpperMaskShapeLayer: CAShapeLayer = {
        var layer = CAShapeLayer()
        layer.frame = self.bounds
        layer.path = self.getBezierPath(radius: outerCircleRadius).cgPath
        layer.lineCap = kCALineCapRound
        layer.strokeStart = 0
        layer.strokeEnd = 0
        layer.lineDashPattern = [NSNumber.init(value: outerDashPatternFirst), NSNumber.init(value: outerDashPatternSecond)]
        layer.backgroundColor = UIColor.clear.cgColor
        layer.lineWidth = outerCircleLineWidth
        layer.strokeColor = UIColor.red.cgColor
        layer.fillColor = UIColor.clear.cgColor
        return layer
    }()
    // 外圈渐变涂层
    private lazy var progressUpperGradientLayer: CAGradientLayer = {
        var layer = CAGradientLayer()
        layer.frame = self.bounds
        layer.shadowPath = self.getBezierPath(radius: outerCircleRadius).cgPath
        layer.backgroundColor = UIColor.clear.cgColor
        layer.startPoint = CGPoint.init(x: 0, y: 1)
        layer.endPoint = CGPoint.init(x: 1, y: 0)
        layer.colors = [strokeColor, strokeColor]
        return layer
    }()
    
    // 复原
    func reset() {
        // 复位遮照层
        upperMaskShapeLayer.strokeEnd = 0
        progressUpperMaskShapeLayer.strokeEnd = 0
        // 重设遮照层
        CATransaction.begin()
        CATransaction.setDisableActions(false)
        CATransaction.setAnimationTimingFunction(CAMediaTimingFunction.init(name: kCAMediaTimingFunctionLinear))
        CATransaction.setAnimationDuration(0.4)

        upperMaskShapeLayer.strokeEnd = percent
        progressUpperMaskShapeLayer.strokeEnd = percent

        CATransaction.commit()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        drawLayers()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        drawLayers()
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    func drawLayers() {
        backgroundColor = .clear
        
        // 内圈
        bottomShapeLayer.addSublayer(upperGradientLayer)
        upperGradientLayer.mask = upperMaskShapeLayer
        self.layer.addSublayer(bottomShapeLayer)
        
        // 外圈
        progressBottomLayer.addSublayer(progressUpperGradientLayer)
        progressUpperGradientLayer.mask = progressUpperMaskShapeLayer
        self.layer.addSublayer(progressBottomLayer)
        
        customization()
    }
    
    open func customization() {
        
    }
    
    func getBezierPath(radius: CGFloat) -> UIBezierPath {
        return UIBezierPath.init(arcCenter: CGPoint.init(x: self.center.x, y: self.center.y ), radius: radius, startAngle: degreesToRadians(startAngle), endAngle: degreesToRadians(endAngle), clockwise: true)
    }
    
    func degreesToRadians(_ degree: CGFloat) -> CGFloat{
        return (CGFloat.pi * degree) / 180.0
    }
}
